package com.liwang.lanouhn.nvshengo.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liwang.lanouhn.nvshengo.Info.LikeEvent;
import com.liwang.lanouhn.nvshengo.Info.TutorialInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.activities.TutorialDetailsActivity;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 2016/10/12.
 */
public class TutorialFragment extends Fragment implements View.OnClickListener {

    private CircleImageView iv_head;
    private TextView tv_title;
    private TextView tv_username;
    private TextView tv_like;
    private TextView tv_look;
    private TextView tv_comment;
    private TextView tv_shop_title;
    private TextView tv_shop;
    private ImageView iv_one;
    private ImageView iv_two;
    private ImageView iv_there;
    private ImageView iv_four;
    private ImageView iv_five;
    private ImageView iv_six;
    private ImageView iv_seven;
    private LinearLayout ll_replace_one;
    private LinearLayout ll_replace_two;
    private LinearLayout ll_replace_there;
    private LinearLayout ll_replace_four;

    public static TutorialFragment newInstants(TutorialInfo info,String like) {
        TutorialFragment tutorialFragment = new TutorialFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("tutorialInfo", info);
        bundle.putSerializable("like",like);
        tutorialFragment.setArguments(bundle);
        return tutorialFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tutorial, null);
        initView(view);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
    private void initView(View view) {
        iv_head = (CircleImageView) view.findViewById(R.id.iv_head);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_username = (TextView) view.findViewById(R.id.tv_username);
        tv_like = (TextView) view.findViewById(R.id.tv_like);
        tv_look = (TextView) view.findViewById(R.id.tv_look);
        tv_comment = (TextView) view.findViewById(R.id.tv_comment);
        TutorialInfo tutorialInfo = (TutorialInfo) getArguments().getSerializable("tutorialInfo");
        Picasso.with(getActivity()).load(tutorialInfo.getAvatar_url()).into(iv_head);
        tv_title.setText(tutorialInfo.getTitle());
        tv_username.setText(tutorialInfo.getUsername());
        String like = (String) getArguments().getSerializable("like");
        tv_like.setText(tutorialInfo.getLike_count()+"");
        tv_look.setText(tutorialInfo.getView_count() + "");
        tv_comment.setText(tutorialInfo.getComment_count() + "");
        tv_shop_title = (TextView) view.findViewById(R.id.tv_shop_title);
        tv_shop_title.setOnClickListener(this);
        tv_shop = (TextView) view.findViewById(R.id.tv_shop);
        tv_shop.setOnClickListener(this);

        ll_replace_one = (LinearLayout) view.findViewById(R.id.ll_replace_one);
        ll_replace_one.setOnClickListener(this);
        ll_replace_two = (LinearLayout) view.findViewById(R.id.ll_replace_two);
        ll_replace_two.setOnClickListener(this);
        ll_replace_there = (LinearLayout) view.findViewById(R.id.ll_replace_there);
        ll_replace_there.setOnClickListener(this);
        ll_replace_four = (LinearLayout) view.findViewById(R.id.ll_replace_four);
        ll_replace_four.setOnClickListener(this);
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);//注册
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//反注册
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLikeEvent(LikeEvent event){
        tv_like.setText(event.text);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.ll_replace_one:
                TutorialInfo tutorialInfo =new TutorialInfo();
                tutorialInfo.setTitle("偷学大饼脸的“减肥妆”");
                tutorialInfo.setAvatar_url("http://st-qn.meiyaapp.com/2015/01/07/12ec0ac2e3608cb5bc0b45e92c3768f51420621472473.jpg");
                tutorialInfo.setUsername("莱苨");
                tutorialInfo.setLike_count(14);
                tutorialInfo.setView_count(67283);
                tutorialInfo.setComment_count(218);
                tutorialInfo.setVideo_url("http://st-up.meiyaapp.com/2015/12/31/o_1a7ranl72ltouahnnf1340u8u3q50738.mp4");
                Intent intent1 =new Intent(getActivity(), TutorialDetailsActivity.class);
                intent1.putExtra("info",tutorialInfo);
                startActivity(intent1);
                break;
            case R.id.ll_replace_two:
                TutorialInfo tutorialInfo1 =new TutorialInfo();
                tutorialInfo1.setTitle("超光感妆容 惹人羡慕");
                tutorialInfo1.setAvatar_url("http://st-tx.meiyaapp.com/2015-08/1440796634-xwmxc-9075adc40854c1ecd3c896b594b90c7b");
                tutorialInfo1.setUsername("Cynthia默小宝");
                tutorialInfo1.setLike_count(16);
                tutorialInfo1.setView_count(64420);
                tutorialInfo1.setComment_count(230);
                tutorialInfo1.setVideo_url("http://st-tx.meiyaapp.com/2016/03/25/o_1aem8mbk416jnq6ro871duq1v6a8r95496.mp4");
                Intent intent2 =new Intent(getActivity(), TutorialDetailsActivity.class);
                intent2.putExtra("info",tutorialInfo1);
                startActivity(intent2);
                break;
            case R.id.ll_replace_there:
                TutorialInfo tutorialInfo3 =new TutorialInfo();
                tutorialInfo3.setTitle("深度剖析少女妆 减龄秘籍终极版Get");
                tutorialInfo3.setAvatar_url("http://st-tx.meiyaapp.com/2016/05/23/o_1ajedfrjs1fr4oai16n46rh16ej732686.png");
                tutorialInfo3.setUsername("BLANC布朗欧尼");
                tutorialInfo3.setLike_count(2);
                tutorialInfo3.setView_count(3214);
                tutorialInfo3.setComment_count(60);
                tutorialInfo3.setVideo_url("http://st-tx.meiyaapp.com/2016/06/30/o_1amg7j07n2uj1tepgnc138a1bp2e910439.mp4");
                Intent intent3 =new Intent(getActivity(), TutorialDetailsActivity.class);
                intent3.putExtra("info",tutorialInfo3);
                startActivity(intent3);
                break;
            case R.id.ll_replace_four:
                TutorialInfo tutorialInfo4 =new TutorialInfo();
                tutorialInfo4.setTitle("性感猫咪妆已上线！老司机教你画眼妆");
                tutorialInfo4.setAvatar_url("http://st-qn.meiyaapp.com/2015/01/09/4fc6db3be1bf8ff0f8baa2706de135cf1420794581941.jpeg");
                tutorialInfo4.setUsername("摇摆吧");
                tutorialInfo4.setLike_count(369);
                tutorialInfo4.setView_count(18354);
                tutorialInfo4.setComment_count(80);
                tutorialInfo4.setVideo_url("http://st-tx.meiyaapp.com/2016/08/30/o_1ard97ju41vt31jnkccq198r19tc1529807.mp4");
                Intent intent4 =new Intent(getActivity(), TutorialDetailsActivity.class);
                intent4.putExtra("info",tutorialInfo4);
                startActivity(intent4);
                break;
        }
    }
}
